Page({
  /**
   * 页面的初始数据
   */
  data: {
    totalPrice: 0,
    selected: false,
    cartList: [],
    showPage: 2,
    disabled: false,
    plain: false,
  },

  //判断购物车的长度是否为0，如果为零下面结算不能跳转
  cartLength() {
    var cartListLength = this.data.cartList.length;
    if (cartListLength < 1) {
      this.setData({
        disabled: !this.data.disabled,
        plain: !this.data.plain,
        showPage: false
      })
      return
    }
  },

  //结算跳转
  toDetail: function () {
    var cartListLength = this.data.cartList.length;
    if (cartListLength < 1) {
      this.setData({
        disabled: !this.data.disabled,
        plain: !this.data.plain,
        showPage: 0
      })
      return
    }
    wx.navigateTo({
      url: '../shopCity-detial/shopCity-detial',
    })
  },
  //随便逛一逛
  buttonTo: function () {
    wx.navigateBack({
      url: "../shopCity/shopCity"
    })
  },
  // 数量-
  bindMinus: function (e) {
    const id = e.currentTarget.dataset.id;
    var cartList = this.data.cartList;
    var num = cartList[id].goods_num;

    if (num <= 1) {
      return false
    }
    num = num - 1;
    cartList[id].goods_num = num;
    this.setData({
      cartList: cartList
    })
    this.getTotalPrice();
    this.resetParentArr(cartList);  //将waterList存在上一个页面的arr里面
    wx.setStorageSync("cart", cartList);
  },
  // 数量+
  bindPlus: function (e) {
    const id = e.currentTarget.dataset.id;
    var cartList = this.data.cartList;
    var num = cartList[id].goods_num;
    num = num + 1;
    cartList[id].goods_num = num;
    this.setData({
      cartList: cartList
    })
    this.getTotalPrice();
    this.resetParentArr(cartList);
    wx.setStorageSync("cart", cartList);
  },

  resetParentArr: function (arr) {
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    //直接调用上一个页面的setData()方法，把数据存到上一个页面中去
    prevPage.setData({
      arr: arr
    });
  },

  // 删除商品
  deleteList: function (e) {
    const id = e.currentTarget.dataset.id;
    var cartList = this.data.cartList;

    //删除商品以后重新缓存数据  
    cartList.splice(id, 1);
    this.setData({
      cartList: cartList,
    });
    this.getTotalPrice();
    this.resetParentArr(cartList); //重新赋值给父级页面购物车arr数据
    var currentNum = 0;
    for (var i in cartList) {
      if (cartList[i]) {
        currentNum++;
      }
    }
    var pages = getCurrentPages();
    var currPage = pages[pages.length - 1];   //当前页面
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      buyNumber: currentNum
    });
    wx.setStorageSync("cart", cartList);
    if (!currentNum) {
      this.setData({
        showPage: 0
      })
    }
  },

  //计算总价
  getTotalPrice() {
    var cartList = this.data.cartList;
    var total = 0;
    for (var i = 0; i < cartList.length; i++) {
      if (!cartList[i]) {
        continue;
      }
      if (cartList[i].selected) {
        total += cartList[i].goods_num * cartList[i].goods_price;
      }
    }
    this.setData({
      totalPrice: total.toFixed(2)
    })
  },


  //TODO:取出缓存数据，计算总金额
  countTotal: function () {
    var cartData = wx.getStorageSync("cart");
    this.setData({
      cartList: cartData
    })
    var cartList = this.data.cartList;
    var total = 0;
    for (var i = 0; i < cartList.length; i++) {
      if (!cartList[i]) {
        continue;
      }
      if (cartList[i].selected) {
        total += cartList[i].goods_num * cartList[i].goods_price;
      }
    }
    //TODO:根据缓存数据渲染页面
    this.setData({
      totalPrice: total.toFixed(2)
    })
  },
  /**
  * 生命周期函数--监听页面加载
  */
  onLoad: function (options) {
    this.setData({
      showPage: parseInt(options.showPage)
    });
    this.countTotal();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})